package com.guxingke.gblog.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.guxingke.gblog.dmo.Article;
import com.guxingke.gblog.dto.ArticleDto;
import com.guxingke.gblog.dto.ArticleLiteDto;
import com.guxingke.gblog.service.ArticleService;
import com.guxingke.gblog.service.impl.ArticleServiceImpl;
import com.guxingke.gblog.util.PictureUtil;
import com.guxingke.gblog.util.PropertiesUtil;
import com.guxingke.gblog.util.StringUtil;
import com.opensymphony.xwork2.ActionSupport;

public class ArticleManageAction extends ActionSupport implements
		ServletRequestAware {
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpSession session;
	private ArticleService articleService = new ArticleServiceImpl();

	List<ArticleLiteDto> articles;
	String mainPage;
	String[] pictures;
	ArticleDto article;
	Article uploadArticle;

	public Article getUploadArticle() {
		return uploadArticle;
	}

	public void setUploadArticle(Article uploadArticle) {
		this.uploadArticle = uploadArticle;
	}

	public List<ArticleLiteDto> getArticles() {
		return articles;
	}

	public void setArticles(List<ArticleLiteDto> articles) {
		this.articles = articles;
	}

	public String getMainPage() {
		return mainPage;
	}

	public void setMainPage(String mainPage) {
		this.mainPage = mainPage;
	}

	public String[] getPictures() {
		return pictures;
	}

	public void setPictures(String[] pictures) {
		this.pictures = pictures;
	}

	public ArticleDto getArticle() {
		return article;
	}

	public void setArticle(ArticleDto article) {
		this.article = article;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	private String error;

	@Override
	public String execute() throws Exception {
		if (!isPass()) {
			return ERROR;
		}
		articles = articleService.getArchive();
		mainPage = "template/article/manage-list.jsp";
		return SUCCESS;
	}

	public String preSave() throws Exception {
		if (!isPass()) {
			return ERROR;
		}
		String imageBasePath = request.getSession().getServletContext()
				.getRealPath("image");
		pictures = PictureUtil.getPictures(imageBasePath,
				PropertiesUtil.getValue("articlePicturePath"));
		String articleId = request.getParameter("articleId");
		if (StringUtil.isNotEmpty(articleId)) {
			article = articleService.getArticle(Integer.parseInt(articleId));
			uploadArticle = new Article();
			uploadArticle.setId(article.getId());
			uploadArticle.setCategoryId(article.getCategory().getId());
			uploadArticle.setPicture(article.getPicture());
			uploadArticle.setTitle(article.getTitle());
			uploadArticle.setContent(article.getContent());
			uploadArticle.setRemark(article.getRemark());
			uploadArticle.setUserId(article.getUser().getId());
		} else {
			uploadArticle = new Article();
		}
		mainPage = "template/article/manage-save.jsp";
		return SUCCESS;
	}

	public String save() throws Exception {
		if (!isPass()) {
			return ERROR;
		}

		if (uploadArticle.getId() != null) {
			articleService.updateArticle(uploadArticle);
		} else {
			uploadArticle.setUserId(1);
			uploadArticle.setClicks(0);
			uploadArticle.setPubDate(new Date());
			uploadArticle.setIsDraft(0);
			System.out.println(uploadArticle);
			articleService.saveArticle(uploadArticle);
		}
		
		return "otherSuccess";
	}

	public String delete() throws Exception {
		if (!isPass()) {
			return ERROR;
		}

		String articleId = request.getParameter("articleId");
		articleService.deleteArticle(Integer.parseInt(articleId));

		return "otherSuccess";
	}

	public String show() throws Exception {
		if (!isPass()) {
			return ERROR;
		}
		String articleId = request.getParameter("articleId");
		article = articleService.getArticle(Integer.parseInt(articleId));
		mainPage = "template/article/manage-show.jsp";
		return SUCCESS;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		this.session = request.getSession();
	}

	private boolean isPass() {
		return session.getAttribute("admin") != null;
	}
}
